import sys

import matplotlib.pyplot as plt
import matplotlib.patches as mpathes
import cv2
import pandas as pd
import numpy as np
import pygame
import sys
from pygame import *

labels = ['x0', 'y0', 'x1', 'y1', 'layer_id', 'net_id']
df = pd.read_csv('./csv/beforebak2.csv', names=labels)

before = df
# 设置要显示的layer
# before = df[(df['layer_id'] == "'6-0'") | (df['layer_id'] == "'9-0'") | (df['layer_id'] == "'10-0'") | (
#         df['layer_id'] == "'11-0'") | (df['layer_id'] == "'12-0'")]
# before = df
df_after = pd.read_csv('./csv/after.csv', names=labels)
# before = df_after

layer_list = list(set(before['layer_id']))
net_list = list(set(before['net_id']))
print("layer_list：", layer_list)
print('net_list：', net_list, '\n')
print(before)
zoom = 50
data = list(zip((before['x0'] + 1000) / zoom, (before['y0'] + 1000) / zoom, (before['x1'] + 1000) / zoom,
                (before['y1'] + 1000) / zoom))

print(data)
xy_list = data
# before_layer_dict = dict(zip(before['layer_id'], before['net_id']))
# after_layer_dict = dict(zip(after['layer_id'], after['net_id']))

num_layer = before['layer_id'].value_counts()
print(num_layer)

# print("before_layer_dict:", before_layer_dict)
# print("after_layer_dict:", after_layer_dict)

pygame.init()
size = (1000, 1000)
WHITE = (255, 255, 255, 0)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0, 100)

PLOY_COLOR = (255, 215, 0)
CONT_COLOR = (65, 105, 225)
MET1_COLOR = (250, 128, 114)
VIA1_COLOR = (106, 90, 205)
MET2_COLOR = (255, 0, 255)

clock = pygame.time.Clock()
screen = pygame.display.set_mode(size, RESIZABLE)
zoom = 30
h_move = 4000
v_move = 4000


def add_label(screen):
    font = pygame.font.SysFont("freesansbold.ttf", 20)
    text_ploy = font.render("PLOY", True, WHITE)
    text_cont = font.render("CONT", True, WHITE)
    text_met1 = font.render("MET1", True, WHITE)
    text_via1 = font.render("VIA1", True, WHITE)
    text_met2 = font.render("MET2", True, WHITE)

    screen.blit(text_ploy, (60, 30))
    screen.blit(text_cont, (60, 60))
    screen.blit(text_met1, (60, 90))
    screen.blit(text_via1, (60, 120))
    screen.blit(text_met2, (60, 150))

    pygame.draw.rect(screen, PLOY_COLOR, (30, 30, 30, 30), 0)
    pygame.draw.rect(screen, CONT_COLOR, (30, 60, 30, 30), 0)
    pygame.draw.rect(screen, MET1_COLOR, (30, 90, 30, 30), 0)
    pygame.draw.rect(screen, VIA1_COLOR, (30, 120, 30, 30), 0)
    pygame.draw.rect(screen, MET2_COLOR, (30, 150, 30, 30), 0)


while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 5:
                zoom = zoom + 4
            elif event.button == 4:
                zoom = zoom - 4
        x, y = pygame.mouse.get_pos()
        print('zoom:', zoom)
        # print(x, y)

        if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
            h_move = h_move + 400
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
            h_move = h_move - 400
        if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
            v_move = v_move - 400
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
            v_move = v_move + 400
    screen.fill(BLACK)
    add_label(screen)
    data = list(zip((before['x0'] + h_move) / zoom, (before['y0'] + v_move) / zoom, (before['x1'] + h_move) / zoom,
                    (before['y1'] + v_move) / zoom, before['layer_id'], before['net_id']))
    xy_list = data
    for x0, y0, x1, y1, layer_id, net_id in xy_list:
        color = WHITE
        width = 1
        if layer_id == "'6-0'":
            color = PLOY_COLOR
            width = 1
        elif layer_id == "'9-0'":
            # BLUE
            color = CONT_COLOR
            width = 1
        elif layer_id == "'10-0'":
            color = MET1_COLOR
            width = 1
        elif layer_id == "'11-0'":
            color = VIA1_COLOR
            width = 1
        elif layer_id == "'12-0'":
            color = MET2_COLOR
            width = 1
        pygame.draw.rect(screen, color, (x0, y0, x1 - x0, y1 - y0), width)
        # font = pygame.font.SysFont("Times New Roman", 15)
        # text_net_id = font.render(net_id, True, WHITE)
        # screen.blit(text_net_id, (x0, y0))
        # pygame.draw.rect(screen, BLACK, (x0, y0, x1 - x0, y1 - y0), 1)

    pygame.display.flip()
    clock.tick(10)
